﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TileEditor01
{
    public partial class TileForm : Form
    {
        public event ApplyEventHandler Apply;
        Tiles tileRef;
        public TileForm(Tiles tileRef)
        {
            InitializeComponent();
            this.tileRef = tileRef;
            tileRef.initSelectionRect(0,0,(int)NUDWidth.Value,(int)NUDHeight.Value);
        }

        private void dbPanel1_Paint(object sender, PaintEventArgs e)
        {
            Point offset = new Point(0, 0);
            offset.X += dbPanel1.AutoScrollPosition.X;
            offset.Y += dbPanel1.AutoScrollPosition.Y;

            e.Graphics.DrawImage(tileRef.bitmap_Player, offset);
            Pen pen = new Pen(Color.Cyan,3);
            e.Graphics.DrawRectangle(pen, new Rectangle(
                tileRef.tileSelectionRect.X + offset.X,
                tileRef.tileSelectionRect.Y + offset.Y,
                tileRef.tileSelectionRect.Width,
                tileRef.tileSelectionRect.Height
                ));


            drawGridLine(e);
        }

        private void drawGridLine(PaintEventArgs e)
        {
            Point offset = new Point(0, 0);
            offset.X += dbPanel1.AutoScrollPosition.X;
            offset.Y += dbPanel1.AutoScrollPosition.Y;

            Pen pen = new Pen(Color.Red);
            int farW = 0;
            while (farW < tileRef.bitmap_Player.Width)
                farW += tileRef.tileSelectionRect.Width;
            int farH = 0;
            while (farH < tileRef.bitmap_Player.Height)
                farH += tileRef.tileSelectionRect.Height;

            for (int x = 0; x <= farW; x += tileRef.tileSelectionRect.Width)
                e.Graphics.DrawLine(pen, x + offset.X, 0 + offset.Y, x + offset.X, farH + offset.Y);
            for (int y = 0; y <= farH; y += tileRef.tileSelectionRect.Height)
                e.Graphics.DrawLine(pen, 0 + offset.X, y + offset.Y, farW + offset.X, y + offset.Y);
        }

        private void dbPanel1_MouseMove(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel1.Text = e.Location.ToString();
        }

        private void NUDHeight_ValueChanged(object sender, EventArgs e)
        {
            tileRef.updateSelectionRectHeight((int)NUDHeight.Value);
            tileRef.clampSelectionRect();
            dbPanel1.Invalidate();
        }

        private void NUDWidth_ValueChanged(object sender, EventArgs e)
        {
            tileRef.updateSelectionRectWidth((int)NUDWidth.Value);
            tileRef.clampSelectionRect();
            dbPanel1.Invalidate();
        }

        private void dbPanel1_MouseDown(object sender, MouseEventArgs e)
        {
            Point offset = new Point(0, 0);
            offset.X += dbPanel1.AutoScrollPosition.X;
            offset.Y += dbPanel1.AutoScrollPosition.Y;

            tileRef.updateSelectionRectLocation(e.X - offset.X, e.Y - offset.Y);
            dbPanel1.Invalidate();
        }

        private void dbPanel1_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void dbPanel1_MouseHover(object sender, EventArgs e)
        {

        }

        private void dbPanel1_MouseUp(object sender, MouseEventArgs e)
        {

        }
        public void setAutoScrollMinSize()
        {
            dbPanel1.AutoScrollMinSize = new Size(tileRef.bitmap_Player.Width, tileRef.bitmap_Player.Height);
            Graphics g = dbPanel1.CreateGraphics();
            tileRef.bitmap_Player.SetResolution(g.DpiX, g.DpiY);
            g.Dispose();
        }

        private void newTitleSetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                tileRef.bitmap_Player = new Bitmap(dlg.FileName);
                setAutoScrollMinSize();
                dbPanel1.Invalidate();
                if (Apply != null)
                {
                    Apply(this, new ApplyEventArgs());
                }
            }
        }
    }

    public delegate void ApplyEventHandler(object sender, ApplyEventArgs e);

    public class ApplyEventArgs : EventArgs
    {
        public ApplyEventArgs()
        {
        }
    }
}
